* Authors:
* Program written by Bolin, Song (松柏林) Shenzhen , China.
* Wechat:songbl_stata, 2021-01-02
* Please do not use this code for commerical purpose


capture program drop cie
program define cie
	version 15.0
syntax anything  (name = tickers )
preserve
local x1=substr("`tickers'",1,4)
local x2=substr("`tickers'",5,6)
local path `c(pwd)'
dis  _col(2) `"{browse "`path'": 点击下载路径}"'
dis ""
  forvalues k =`x1'/`x1'{  
  while length("`tickers'") !=6 {
			disp as error `"`tickers' is a set of invalid numbers, please re-enter。"'
			exit 
		} 
	while `tickers'< 201610 {
	disp as error `"Note: the code of "China's industrial economy" has been published since the 10th issue of 2016, please re-enter"'
			exit 
		} 
  qui forvalues j = `x2'/`x2'{ 
    while `j'== 0 | `j'>12 {
       disp as error `"Note: there is no issue `j' in China industrial economy, please enter 1-12."'
			exit 
    }
    cd `"`path'"'
    cap mkdir "`k'-`j'"
    cd `"`k'-`j'"'
    cap copy "http://ciejournal.ajcass.org/Magazine/GetIssueContentList?Year=`k'&Issue=`j'"  temp.txt, replace
    while _rc != 0 {
        sleep 5000
         cap copy "http://ciejournal.ajcass.org/Magazine/GetIssueContentList?Year=`k'&Issue=`j'"  temp.txt, replace
    }
    infix strL v 1-100000 using temp.txt, clear
    keep if index(v, "/Magazine/show/?id=")
    cap split v, p("id=")
	  while _rc != 0{
	  erase temp.txt
	  clear all
	  cd `"`path'"'
	  rmdir "`k'-`j'"
	  disp as error `"Note: `k' year issue `j' of China industrial economy has not been updated"'
	  exit 
	  }
    split v2,p(">")
    gen id =  substr(v2,1,5)
    gen name =  v22
    keep name id  
    destring id, replace 
	
      if id<= 51367 {
        levelsof id, local(number)
        foreach  i  in  `number' { 
          cap copy "http://ciejournal.ajcass.org/Magazine/Show?id=`i'" temp.txt, replace    
          infix strL v 1-100000 using temp.txt, clear
          keep if index(v, "UploadFile")  | index(v, "044693") 
          replace v = v + "?" + v[_n + 1] + "?" + v[_n + 2] 
          keep in 1 
          split v, p("?") 
          drop v
          cap keep v1 v2 v3
            while  _rc != 0 {
              cap gen v3=v2 
              cap  drop v2
            } 
         split v1, p(".")
         split v11, p("/")
         cap split v2, p(".")
         cap split v21, p("/") 
         split v3, p(">")  
         split v32, p("<") 
         cap keep v114 v214 v321
           if  _rc != 0{   
             keep v114  v321 
             gen paper_url="http://ciejournal.ajcass.org/UploadFile/Issue/"+v114+".pdf"
             gen name=v321 
             gen id=`j'
             keep paper_url  name id
             local x =name[1]
             local y =paper_url[1]
             cap mkdir "`x'" 
	         cd `"`x'"'         
			 cap copy "`y'"  "`x'.pdf" ,replace 
			   while _rc != 0 {
                sleep 5000
                cap copy "`y'"  "`x'.pdf" ,replace
              }
          } 
            cd `"`path'"'
			cd `"`k'-`j'"'   
          else{
            keep v114 v214 v321 
            gen  paper_url="http://ciejournal.ajcass.org/UploadFile/Issue/"+v114+".pdf"
            gen  file_url="http://ciejournal.ajcass.org/UploadFile/Issue/"+v214
            gen  name=v321 
            gen  id=`j'
            keep paper_url file_url name id
            replace file_url=file_url+".rar" 
            local x =name[1]
            local y =paper_url[1]
            cap mkdir "`x'" 
	        cd `"`x'"'         
			cap copy "`y'"  "`x'.pdf" ,replace
			  while _rc != 0 {
                sleep 5000
                cap copy "`y'"  "`x'.pdf" ,replace
              }
            local x =name[1]
            local y =file_url[1]
            cap copy "`y'"  "`x'.rar" ,replace
			  while _rc != 0 {
                sleep 5000
                cap copy "`y'"  "`x'.rar" ,replace
              }
          } 
          cd `"`path'"'
		  cd `"`k'-`j'"' 
      }
 
      }
     
	 else {
       levelsof id, local(number)
         foreach  i  in  `number' { 
           cap copy "http://ciejournal.ajcass.org/Magazine/Show?id=`i'" temp.txt, replace
           infix strL v 1-100000 using temp.txt, clear
           keep if index(v, "UploadFile")  | index(v, "044693") 
           replace v = v + "?" + v[_n + 1] + "?" + v[_n + 2] 
           keep in 1 
           split v, p("?") 
           drop v
           cap keep v1 v2 v3
             while  _rc != 0 {
               cap gen v3=v2 
               cap drop v2
             } 
           split v1, p(".")
           split v11, p("/")
           cap split v2, p(".")
           cap split v21, p("/") 
           split v3, p(">")  
           split v32, p("<")  
           cap keep v114 v214 v321
             if  _rc != 0{   
               keep v114  v321 
               gen paper_url="http://ciejournal.ajcass.org/UploadFile/Issue/"+v114+".pdf"
               gen name=v321 
               gen id=`j'
               keep paper_url  name id
               local x =name[1]
               local y =paper_url[1]
			   cap mkdir "`x'" 
	           cd `"`x'"'         
               cap copy "`y'"  "`x'.pdf" ,replace 
			     while _rc != 0 {
                   sleep 5000
                   cap copy "`y'"  "`x'.pdf" ,replac
                 }
             }  
             cd `"`path'"'
			 cd `"`k'-`j'"' 
			 
			 else{
               keep v114 v214 v321 
               gen  paper_url="http://ciejournal.ajcass.org/UploadFile/Issue/"+v114+".pdf"
               gen  file_url="http://ciejournal.ajcass.org/UploadFile/Issue/"+v214
               gen  name=v321 
               gen id=`j'
               keep paper_url file_url name id
               replace file_url=file_url+".zip" 
               local x =name[1]
               local y =paper_url[1]              			 
               cap mkdir "`x'" 
	           cd `"`x'"'         
			   cap copy "`y'"  "`x'.pdf" ,replace 
			   while _rc != 0 {
                    sleep 5000
                    cap copy "`y'"  "`x'.pdf" ,replace 
                  }
               local x =name[1]
               local y =file_url[1]
               cap copy "`y'"  "`x'.rar" ,replace
			      while _rc != 0 {
                    sleep 5000
                     cap copy "`y'"  "`x'.rar" ,replace
                  }
             }  
              cd `"`path'"'
			  cd `"`k'-`j'"' 
         }
     }    
  
  } 
erase temp.txt
  
  }  
  dis as blue "The download is complete"
  restore
 qui cd `"`path'"'
  view browse  `"`path'"'
end
